package walker

import (
	"encoding/json"
	"fmt"
	"iptv_server/v3/src/models/homevideo_model"
	"iptv_server/v3/src/settings"
	"iptv_server/v3/wallk/src/walker/m"
	"os"
)

// "iptv_server/v3/src/models/homevideo_model"
// _ = new(homevideo_model.Home)

type IDS struct {
	// site id
	// group id
	// vod id
	// play id (video url id)
	gid, vid, pid uint
}

func saveGroups(gps []*group, sid uint, ids *IDS, parentGid uint) {
	// 第一层 site id >1 ; parentGid=0
	//
	if len(gps) == 0 {
		return
	}

	var currentGid uint

	fmt.Printf("[I] group length=len(gps): %v\n", len(gps))
	for j, g := range gps {
		fmt.Printf("[I]=======:group%v:%s\n", j, g.Name)
		var followTo uint = 0
		if g.FollowedTo > 0 {
			// 只存在 于 第一层目录

			// 组 已经存在, 对其扩充!!!
			// 不改变 gid编号
			followTo = uint(g.FollowedTo)
			currentGid = followTo
		} else {

			ids.gid++
			currentGid = ids.gid

			if sid == 0 { //site id==0, ->子目录
				ids.vid++
				// todo : 给 每个 sub group 建一个 vod

				var vodId string
				if len(g.SubId) > 0 {
					vodId = g.SubId
				} else {
					vodId = fmt.Sprintf("%d", currentGid)
				}
				detail := &homevideo_model.Detail{
					ID:         ids.vid, // 主键
					VodId:      vodId,   // vod id
					CategoryID: parentGid,
					Name:       g.Name,
					Pic:        g.Pic, //todo => ok
					// \重定向sub group
					IsFolder: 1, // is子目录 的标记
				}
				/// !这个 sub组 不该存在 detail表
				/// 新建一个表.
				///  存一起==增加复杂度
				detail.Save()
			}

			// 新建组.
			cate := &homevideo_model.Category{
				ID:     currentGid,
				Type:   2,
				HomeID: sid,
				Name:   g.Name,
				Dir:    g.Dir,
			}

			if err := cate.Save(); err != nil {
				fmt.Printf("cate.Save err: %v\n", err)
			}
		}

		// 子分组 优先
		gps2 := g.SubGroups

		// gid 会随着 sub组增长, 先存起来,供下面的 vod使用
		// currentGid := ids.gid
		if len(gps2) > 0 {
			saveGroups(gps2, 0, ids, currentGid)
		}

		for k, vod := range g.Vods {
			fmt.Printf("k: %v\n", k)
			fmt.Printf("vod.Dir: %v\n", vod.Dir)
			fmt.Printf("vod.Name: %v\n", vod.Name)
			fmt.Printf("len(vod.VideoPaths): %v\n", len(vod.VideoPaths))
			fmt.Printf("vod.InfoJsonPath: %v\n", vod.InfoJsonPath)
			fmt.Printf("vod.Pic: %v\n", vod.Pic)
			ids.vid++
			// TODO read json

			// 豆瓣 存的数据 跟这个 json tag 对不上!!!
			douban := &m.DoubanDetail{}
			if len(vod.InfoJsonPath) > 0 {
				bs, err := os.ReadFile(vod.InfoJsonPath)
				if err == nil {
					json.Unmarshal(bs, douban)
				}
			}
			// detail := &homevideo_model.Detail{}
			detail := douban.ToModelDetail()

			detail.ID = ids.vid
			detail.CategoryID = currentGid
			if len(detail.Name) == 0 {
				detail.Name = vod.Name
			}

			detail.VodId = fmt.Sprintf("%d", detail.ID)
			detail.Pic = vod.Pic

			// 这里都是 ==0; 默认值 在json中 可以 omitempty
			// 需要重定向的 是 sub group
			detail.IsFolder = 0

			detail.Save()

			for _, v := range vod.VideoPaths {
				ids.pid++
				// 00000000000xn
				u := v.Path
				title, season_n := videoTitleN(v.Path, v.Title)
				if season_n > 0 {
					u = settings.GetSeasonUrlI(season_n)
					// u = settings.GetSeasonPathI(season_n)
				}
				play := &homevideo_model.Play{
					ID:       ids.pid,
					Name:     title,
					DetailID: ids.vid,
					Url:      u,
					// PlayerArgs: ??,
				}
				play.Save()
			}
		}
	}
}
